package com.chinapost.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ModelDriven;
import com.chinapost.vo.Json;
import com.chinapost.vo.Jfsz;
import com.chinapost.vo.SessionInfo;
import com.chinapost.service.DeptServiceI;
import com.chinapost.service.JfszServiceI;

@Action(value="jfszAction",results = { @Result(name = "jfszManager", location = "/jsp/jfsz/jfszManager.jsp"), 
									   @Result(name = "jfszAdd", location = "/jsp/jfsz/jfszAdd.jsp"), 
									   @Result(name = "jfszEdit", location = "/jsp/jfsz/jfszEdit.jsp") })
public class JfszAction extends BaseAction implements ModelDriven<Jfsz>, ServletRequestAware {
	
	private Jfsz jfsz=new Jfsz();
	private HttpServletRequest request;
	@Autowired
	private JfszServiceI jfszService;
	@Autowired
	private DeptServiceI deptService;
	

	@Override
	public Jfsz getModel() {
		return jfsz;
	}

	public String jfszManager(){
		return "jfszManager";
	}
	
	public String jfszAdd(){
		return "jfszAdd";
	}
	
	public String jfszEdit(){
		return "jfszEdit";
	}
	
	public void add(){
		Json j = new Json();
		try {
			jfszService.add(jfsz);
			j.setSuccess(true);
			j.setMsg("添加成功！");
		} catch (Exception e) {
			j.setMsg("添加失败！");
		}
		super.writeJson(j);
	}
	
	public void datagrid() {
		if (!StringUtils.isEmpty(jfsz.getDeptid())) {
			jfsz.setDeptIds(deptService.getChildIds(jfsz.getDeptid()));
		} else {
			SessionInfo s = (SessionInfo)request.getSession().getAttribute("sessionInfo");
			jfsz.setDeptIds(deptService.getChildIds(s.getDeptid()));
		}

		super.writeJsonDate(jfszService.datagrid(jfsz));
	}
	
	public void delete(){
		Json j = new Json();
		try {
			jfszService.delete(jfsz);
			j.setSuccess(true);
			j.setMsg("删除成功！");
			j.setObj(jfsz.getCid());
		} catch (Exception e) {
			j.setMsg("删除失败！");
		}
		super.writeJson(j);
	}
	
	public void edit(){
		Json j = new Json();
		try {
			jfszService.edit(jfsz);
			j.setSuccess(true);
			j.setMsg("编辑成功!");
		} catch (Exception e) {
			j.setMsg("编辑失败！");
		}
		writeJson(j);
	}
	
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;

	}
	
}
